<?php
/**
 * Created by PhpStorm.
 * User: Asnyc
 * Date: 2017/12/14
 * Time: 16:38
 * class: 商家
 */

namespace app\api\controller\v1;

use app\api\model\Store as StoreModel;
use app\api\validate\IDMustBePostiveInt;
use app\api\validate\StoreSearch;
use app\lib\exception\NotFindException;
use app\lib\exception\StoreMissException;

class Store
{
    /**
     * @param $id
     * @return \think\response\Json
     * @throws StoreMissException
     * 获取特定id的商家信息
     */
    public function getStore($id){
        //数据验证,通过就往下执行否则抛出异常
        (new IDMustBePostiveInt())->gocheck();
        //得到商家信息
        $store = StoreModel::getstorebyid($id);
        if(!$store){
            throw new StoreMissException(); //抛出自定义异常
        }
        $info = [
            'code' => 200,
            'msg'  => '商家信息数据获取成功',
            'data' => $store
        ];
        return json($info,200);
    }

    /**
     * @return \think\response\Json
     * @throws StoreMissException
     * 获取所有商家信息func
     */
    public function getAllStore(){
       $allstore = StoreModel::getallstore();
        if(!$allstore){
            throw new StoreMissException(); //抛出自定义异常
        }
        $info = [
            'code' => 200,
            'msg'  => '全部商家信息数据获取成功',
            'data' => $allstore
        ];
        return json($info,200);
    }

    /**
     * @param $id
     * @return \think\response\Json
     * @throws StoreMissException
     * 根据商家id得到该商家信息及菜单func
     */
    public function getMenu($id){
        //数据验证,通过就往下执行否则抛出异常
        (new IDMustBePostiveInt())->gocheck();
        $menu = StoreModel::with('getStoreMenu')->find($id);
        if(!$menu){
            throw new StoreMissException(); //抛出自定义异常
        }
        return json($menu,200);
    }

    /**
     * @return \think\response\Json
     * @throws NotFindException
     * 搜索商家
     */
    public function searchStore(){
        (new StoreSearch())->gocheck();
        $store = StoreModel::search();
        if(!$store){
            throw new NotFindException([
                'msg' => '哎呀,没有搜索到商家,大概是被外星人带走了Σ(ﾟдﾟlll)'
            ]);
        }else{
            return json($store,200);
        }
    }
}